﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAdminBusinessLayer;
using AuthorObj = AlexandriaPressAdminObject.AuthorInfo;
using MailSender;
using AlexandriaPressUserBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm11 : System.Web.UI.Page
    {

        protected void Page_Init(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.Name != "")
            {
                UserBusinsessLayer business = new UserBusinsessLayer();
                string _UserType = business.getUserType(HttpContext.Current.User.Identity.Name);
                if (_UserType != "Type_Admin")
                {
                    Response.Redirect("~/MasterPages/ErrorPage.aspx");
                }
            } 
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                view(false, false);
            }
            lblDOS.Visible = false;
            lblCheck.Visible = false;


        }

        private void view(bool calendar, bool comment)
        {
            Label1.Visible = calendar;
            Calendar1.Visible = calendar;
            Label2.Visible = comment;
            TextBox3.Visible = comment;
            btnBan.Visible = false;
            btnSuspend.Visible = false;

        }

        private void BindData()
        {
            gridAuthorList.DataSource = AuthorBL.viewAuthorList();
            gridAuthorList.EmptyDataText = "No author in the list";
            gridAuthorList.DataBind();
        }

        protected void btnBan_Click(object sender, EventArgs e)
        {
            int countAll;
            int countChecked = 0;
            List<AuthorObj> author = new List<AuthorObj>();
            AuthorObj authObj;
            for (int i = 0; i < gridAuthorList.Rows.Count; i++)
            {
                CheckBox selected = (CheckBox)gridAuthorList.Rows[i].FindControl("cbSelect");

                if (selected.Checked)
                {
                    authObj = new AuthorObj();
                    authObj.ID = Convert.ToInt32(gridAuthorList.DataKeys[i]["ID"].ToString());
                    authObj.Email = Convert.ToString(gridAuthorList.DataKeys[i]["Email"].ToString());
                    authObj.Reason = TextBox3.Text;
                    EmailNotification.AdminMail("admin@alexandria.com", authObj.Email, null, null, "Ban notice", "You are banned effective today, " + DateTime.Now.ToString() + "due to the ff reasons:" + authObj.Reason);
                    author.Add(authObj);
                    
                    countChecked++;

                }
            }

            if (countChecked++ > 0)
            {

                countAll = AuthorBL.BanAuthor(author);
              
                BindData();
                EraseInput();
                lblCheck.Text = countAll + " author/s banned";
                lblCheck.Visible = true;
                if (countAll != 0)
                { 
                
                
                }
            }
            else
            {
                lblCheck.Text = "Please select the author/s you want to ban";
                lblCheck.Visible = true;
            }

        }

        protected void btnSuspend_Click(object sender, EventArgs e)
        {
            int countAll;
            int countChecked = 0;
            if (Calendar1.SelectedDate != null)
            {
                DateTime endDate = Calendar1.SelectedDate;                
                if (endDate > DateTime.Now)
                {
                    List<AuthorObj> author = new List<AuthorObj>();
                    AuthorObj authObj;
                    for (int i = 0; i < gridAuthorList.Rows.Count; i++)
                    {
                        CheckBox selected = (CheckBox)gridAuthorList.Rows[i].FindControl("cbSelect");
                        if (selected.Checked)
                        {
                            authObj = new AuthorObj();
                            authObj.ID = Convert.ToInt32(gridAuthorList.DataKeys[i]["ID"].ToString());
                            authObj.Email = Convert.ToString(gridAuthorList.DataKeys[i]["Email"].ToString());
                            authObj.Reason = TextBox3.Text;
                            authObj.EndOfSuspension = endDate;
                            EmailNotification.AdminMail("admin@alexandria.com", authObj.Email, null, null, "Suspension notice", "You are suspended effective today, " + DateTime.Now.ToString() + " until " + authObj.EndOfSuspension + ", due to the ff reasons:" + authObj.Reason);
                            author.Add(authObj);
                            countChecked++;
                        }
                    }

                    if (countChecked++ > 0)
                    {
                        countAll = AuthorBL.SuspendAuthor(author);
                        BindData();
                        EraseInput();
                        lblCheck.Text = countAll + " author/s suspended";
                        lblCheck.Visible = true;
                    }
                    else
                    {
                        lblCheck.Text = "Please select the author/s you want to suspend";
                        lblCheck.Visible = true;

                    }
                }
                else
                {
                    lblDOS.Visible = true;

                }
            }

        }

        private void EraseInput()
        {
            TextBox3.Text = null;
            lblDOS.Visible = false;
            DropDownList4.SelectedIndex = 0;
        }

        protected void gridAuthorList_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridAuthorList.Visible = true;
            gridAuthorList.PageIndex = e.NewPageIndex;
            gridAuthorList.DataSource = AuthorBL.viewAuthorList();
            gridAuthorList.DataBind();
        }


        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {
            if (TextBox3.Text != null)
            {
                if (DropDownList4.SelectedIndex == 1)
                {
                    btnBan.Visible = true;
                    btnSuspend.Visible = false;
                }
                else if (DropDownList4.SelectedIndex == 2)
                {
                    btnSuspend.Visible = true;
                    btnBan.Visible = false;
                }
                else
                {
                    btnBan.Visible = false;
                    btnSuspend.Visible = false;
                }
            }
            else
            {
                btnBan.Visible = false;
                btnSuspend.Visible = false;
            }

        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList4.SelectedIndex == 1)
            {
                view(false, true);
            }
            else if (DropDownList4.SelectedIndex == 2)
            {
                view(true, true);
            }
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            DateTime date = Calendar1.SelectedDate;
            if (date < DateTime.Now)
            {
                lblDOS.Visible = true;
            }
        }
    }
}
